package com.xmread.cn.network;

import android.util.Base64;
import android.util.Log;

import com.liuguang.framework.utils.SafeSignUtil;
import com.liuguang.framework.utils.StringUtils;
import com.sh.suhe.utildemos.utils.JsonUtil;
import com.xmread.cn.config.AppConfig;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import okhttp3.MediaType;
import okhttp3.MultipartBody.Part;
import okhttp3.RequestBody;

public class ParamsUtil {
    public static final String MULTIPART_FORM_DATA = "image/jpg";

    public static Map<String, Object> getDefaltParams() {
        return new HashMap();
    }

    public static Map<String, Object> setSignParams(Map<String, Object> map) {
        String map2urlParams = map2urlParams(map);
        if (StringUtils.isNotBlank(map2urlParams)) {
            map2urlParams = paramsSort(map2urlParams);
        }
        map.put("sign", Base64.encodeToString(SafeSignUtil.HmacSHA1Encrypt(map2urlParams, AppConfig.SIGN_KEY), 0).trim());
        Log.e("ParamsUtil", JsonUtil.object2Json(map));
        return map;
    }

    public static Part prepareFilePart(String str, String str2) {
        File file = new File(str2);
        return Part.createFormData(str, file.getName(), RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file));
    }

    public static Part prepareFileParts(String str, String str2) {
        File file = new File(str2);
        return Part.createFormData(str, file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
    }

    public static Part prepareFileParts() {
        return Part.createFormData("sign", Base64.encodeToString(SafeSignUtil.HmacSHA1Encrypt(paramsSort(map2urlParams(getDefaltParams())), AppConfig.SIGN_KEY), 0).trim());
    }

    public static String paramsSort(String str) {
        List asList = Arrays.asList(str.split("&"));
        Collections.sort(asList);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < asList.size(); i++) {
            stringBuffer.append((String) asList.get(i));
            if (i == asList.size() - 1) {
                break;
            }
            stringBuffer.append("&");
        }
        return stringBuffer.toString();
    }

    public static String map2urlParams(Map<String, Object> map) {
        if (map == null) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (Entry entry : map.entrySet()) {
            stringBuffer.append((String) entry.getKey());
            stringBuffer.append("=");
            stringBuffer.append(entry.getValue());
            stringBuffer.append("&");
        }
        String stringBuffer2 = stringBuffer.toString();
        String str = "";
        if (stringBuffer2.length() > 0) {
            str = stringBuffer2.substring(0, stringBuffer2.length() - 1);
        }
        return str;
    }

    public static HashMap<String, String> getUrlParams(String str) {
        try {
            String query = new URL(str).getQuery();
            if (!StringUtils.isNotEmpty(query)) {
                return null;
            }
            String[] strArr = {query};
            if (query.contains("&")) {
                strArr = query.split("&");
            }
            HashMap<String, String> hashMap = new HashMap<>();
            for (String split : strArr) {
                String[] split2 = split.split("=");
                if (split2.length == 2) {
                    hashMap.put(split2[0], split2[1]);
                }
            }
            return hashMap;
        } catch (MalformedURLException unused) {
            if (!str.contains("?")) {
                return null;
            }
            String substring = str.substring(str.indexOf("?") + 1);
            String[] strArr2 = {substring};
            if (substring.contains("&")) {
                strArr2 = substring.split("&");
            }
            HashMap<String, String> hashMap2 = new HashMap<>();
            for (String split3 : strArr2) {
                String[] split4 = split3.split("=");
                if (split4.length == 2) {
                    hashMap2.put(split4[0], split4[1]);
                }
            }
            return hashMap2;
        }
    }

    public static String removeUrlParam(String str, String str2) {
        HashMap urlParams = getUrlParams(str);
        if (urlParams == null || !urlParams.containsKey(str2)) {
            return str;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("&");
        sb.append(str2);
        sb.append("=");
        sb.append((String) urlParams.get(str2));
        String replace = str.replace(sb.toString(), "");
        StringBuilder sb2 = new StringBuilder();
        sb2.append("?");
        sb2.append(str2);
        sb2.append("=");
        sb2.append((String) urlParams.get(str2));
        return replace.replace(sb2.toString(), "");
    }
}
